var instrument =[];
$('#showPicker').on('click', function () {

    var la=[];
    for (var i = 0; i < instrument.length; i++) {
        la.push({
            label:instrument[i].name,
            value: (instrument[i].id - 1)
        })
    }

    weui.picker(la, {
        onChange: function (result) {
            console.log(result);
        },
        onConfirm: function (result) {
            showPicker.value=instrument[result].name;
            console.log(showPicker.value);
            if(showPicker.value=="暂无"){
                $('#time').hide();
                $('#line').hide();
                /*
                 $('#time').style.display = "none";
                 $('#line').style.display = "none";
                 */
            }else{
                $('#time').show();
                $('#line').show();
            }
            console.log(result);
        }
    });
});

$('#showDatePicker').on('click', function () {
    weui.datePicker({
        start: 1990,
        end: new Date().getFullYear(),
        onChange: function (result) {
            console.log(result);
        },
        onConfirm: function (result) {
            showDatePicker.value = result[0]+"-"+result[1]+"-"+result[2];
            console.log(result);
        }
    });
});

$('#showHowLongTime').on('click', function () {
    /*time的数据要改成从后端获取，即从json加载，这里需要用ajax获取之后再放到数组中*/
    $.get("student_info.json",function (res, err) {
        var items =  getStudyDurationMap(res);
        if(!items) return;  //如果没有数据，返回
        var time = [items.none,items.beginner,items.junior,items.middle,items.senior];
        weui.picker([{
            label: time[0],
            value: "0"
        }, {
            label: time[1],
            value: "1"
        }, {
            label: time[2],
            value: "2"
        }, {
            label: time[3],
            value: "3"
        }, {
            label: time[4],
            value: "4"
        },], {
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result){
                $('#showHowLongTime').val(time[result]);
                /* showHowLongTime.value=time[result];*/
                /* 如果乐器选了暂无，这里设置字符串空*/
                console.log(result);
                if(showPicker.value=="暂无"){
                    /*                        document.getElementById("time").style.display = "none";*/
                    $('#showHowLongTime').val("无");
                }
            }
        });
    });


});

function checkTelephone(obj){
    var reg= /^[1][3578]\d{9}$/; //验证手机号码
    var phone=document.getElementById('telephone').value;
    if(!reg.test(phone)){
        /*弹窗操作
         var $androidDialog2 = $('#androidDialog2');
         $('#showAndroidDialog2').on('click', function(){
         $androidDialog2.fadeIn(200);
         });*/
        console.log("电话号码格式错误!");
        obj.value="";
        obj.focus();
        return false;
    }else{
        /*    alert("您的电话号码:"+phone);*/
        return true;
    }
}

/* 上面部分是原页面的js，下面是ajax拿json数据的代码；*/

/*获取json内容*/
function getData(result) {
    if(result.code == 0){
        return result.data.student;
    }else{
        alert(result.message||"获取信息错误");
        return null;
    }
}

function getStudyDurationMap(result) {
    if(result.code == 0){
        return result.data.studyDurationMap;
    }else{
        alert(result.message||"获取信息错误");
        return null;
    }
}

function getSubjectList(result) {
    if(result.code == 0){
        return result.data.subjectList;
    }else{
        alert(result.message||"获取信息错误");
        return null;
    }
}

/*页面ajax从json获取内容*/
$.get("student_info.json",function (result, err) {
    var items =  getData(result);

    instrument =getSubjectList(result);

    if(!items) return;  //如果没有数据，返回
    /*将json数据填充到input框里显示*/
    $('#telephone').val(items.mobile);
    $('#showDatePicker').val(items.birthday);

    /*根据subjectId的值用ajax从json拿具体值，赋值给instrument*/

    var hehe =  getSubjectList(result);
    if(!hehe) return;  //如果没有数据，返回
    var myInstrument = hehe[items.subjectId].name;
    $('#showPicker').val(myInstrument);

    var dataStudyDurationMap=getStudyDurationMap(result);
    var myTime = items.studyDuration;
    $('#showHowLongTime').val(dataStudyDurationMap[myTime]);

    if($('#showPicker').val()!="暂无"){
        $('#time').show();
        $('#line').show();
    }else{
        $('#time').css('display','none');
        $('#line').css('display','none');
        /*$('#time').hide();
        $('#line').hide();*/
    }
});
